package com.example.springbootusecase.filter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

/**
 * @author Think
 */
public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 初始化操作
        System.out.println("Filter initialized");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // 在请求处理之前执行的代码
        System.out.println("Before request processing");

        // 继续下一个filter或目标servlet
        chain.doFilter(request, response);

        // 在请求处理之后执行的代码
        System.out.println("After request processing");
    }

    @Override
    public void destroy() {
        // 清理操作
        System.out.println("Filter destroyed");
    }
}